# -*- coding:utf-8 -*-
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution:
    def FindFirstCommonNode(self, pHead1, pHead2):
        # write code here
        # 解决的关键在于，如果从头遍历，那么公共节点前节点的个数两个链表不一定相同，但是从
        # 后往前找的话，节点个数就会相同
        pHead1_list = []
        pHead2_list = []
        while pHead1:
            pHead1_list.append(pHead1)
            pHead1 = pHead1.next
        while pHead2:
            pHead2_list.append(pHead2)
            pHead2 = pHead2.next
        res = None
        while pHead1_list and pHead2_list:
            res1 = pHead1_list.pop()
            res2 = pHead2_list.pop()
            if  res1 == res2:
                res = res1
        return res